In [ ]:

1. Basic Exception Handling


In [2]:
# basic try/except
def is_int(val):
    try:
        x = int(val)
        return True
    except ValueError:
        return False

is_int('erik')


Out[2]:
False

In [3]:
# finally
def is_int(val):
    try:
        x = int(val)
        return True
    except ValueError:
        return False
    finally:
        print('done')
        
is_int('3')


done
Out[3]:
True

In [4]:
is_int('erik')


done
Out[4]:
False

2. Getting Exception Details


In [11]:
def is_int(val):
    try:
        x = int(val)
        return True
    except ValueError as e:     #e is the handled exception
        print(e)
        return False

is_int('erik')


invalid literal for int() with base 10: 'erik'
Out[11]:
False

3. Advanced Exception Handling


In [15]:
# multiple except blocks are tried in order, until a match is found
def is_int(val):
    try:
        x = int(val)
        return True
    except TypeError:
        print('caught TypeError')
        return False
    except Exception:
        print('caught Exception')  # this one is caught
        return False
    except ValueError as e:     
        print('caught ValueError')
        return False

is_int('e')


caught Exception
Out[15]:
False

In [14]:
# beause of above, you should order except blocks from most specific to least
def is_int(val):
    try:
        x = int(val)
        return True
    except ValueError:
        print('caught ValueError')
        return False
    except Exception as e:     
        print('caught Exception')
        return False

is_int('e')


caught ValueError
Out[14]:
False

In [16]:
# getting code to run if NO exception is raised
def is_int(val):
    try:
        x = int(val)
    except ValueError:
        print('caught ValueError')
        return False
    else:
        print('no error')
        return True

is_int('10')


no error
Out[16]:
True

In [17]:
# having two exceptions use the same except block
def is_int(val):
    try:
        x = int(val)
    except (TypeError, ValueError):
        print('caught ValueError or TypeError')
        return False
    else:
        print('no error')
        return True

is_int('erik')


caught ValueError or TypeError
Out[17]:
False

In [18]:
# catching all exceptions

def is_int(val):
    try:
        x = int(val)
        return True
    except Exception:    # use Exception, which handles all exceptions that are not supposed to automatically kill the app
        return False

is_int('erik')


Out[18]:
False

4. Raising Exceptions


In [43]:
# raising a simple exception

def raise_on_non_string(val):
    if not isinstance(val, str):
        raise TypeError('Expected a string')
        
try:
    raise_on_non_string(34)
    
except Exception as e:
    print(e)


Expected a string

In [42]:
# catching an exception, wrapping it with your own and re-throwing
def wrap_raise_on_non_string(val):
    try:
        return raise_on_non_string(val)
    except TypeError as e:
        raise RuntimeError('Wrapping error....') from e

In [55]:
# catching and rethrowing
def wrap_raise_on_non_string(val):
    try:
        return raise_on_non_string(val)
    except TypeError:
        print('TYPE ERROR')
        raise

try:
    wrap_raise_on_non_string(34)
except Exception as e:
    print(e)


TYPE ERROR
Expected a string

In [ ]: